﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data.SqlClient;
using IExternalDataTypes;

namespace ExaminationTT.Control
{
    public class InFilter : IFilter
    {
        private readonly IEnumerable<string> _inParams;
        private readonly List<SqlParameter> _params;
        private readonly string _field;
        private string _whereClause;

        public InFilter(IEnumerable<string> InParams, string field)
        {
           
            _field = field;
            _inParams = InParams.ToList();
            _params = new List<SqlParameter>();
        }
        #region IFilter Members

        public string GetWhereClause()
        {
            if (_whereClause != null) return _whereClause;
            StringBuilder ret = new StringBuilder(" where "+_field+" in (");
           
            foreach (string acr in _inParams)
            {
                ret.Append(" @" + acr + " ,");
            }
            if (_inParams.Count() > 0)
            {
                //remove , 
                ret.Remove(ret.Length - 1, 1);
                ret.Append(")");
            }
            else { ret.Append("'')"); }
            _whereClause = ret.ToString();
            return _whereClause;
        }

        public void BindValues(System.Data.SqlClient.SqlCommand cmd)
        {
            foreach (string p in _inParams)
            {
                cmd.Parameters.AddWithValue("@" + p, p);
            }
            
            //cmd.Parameters.AddRange(_params.ToArray());
            //string query = cmd.CommandText;

            //foreach (SqlParameter p in cmd.Parameters)
            //{
            //    query = query.Replace(p.ParameterName, "'" + p.Value.ToString() + "'");
            //}
            //cmd.CommandText = query;
            //cmd.CommandText = "select ID, Acronym, Name from CUnit where Acronym in ('EGP','PC','PS')";
        }
        

        #endregion
    }

    public class ExternalToInternalObjects
    {
        


        #region simpleParse
        public static void StudentParse(IExternalDataTypes.IStudent s1, Student s2)
        {
            s2.Name = s1.Name;
            s2.Number = s1.Number;
            
        }
        public static void TeacherParse(IExternalDataTypes.ITeacher t1, Teacher t2)
        {
            t2.Name = t1.Name;
            //falta CUnits
        }
        public static void SemesterParser(IExternalDataTypes.ISemester s1, Semester s2)
        {
            s2.Description = s1.Description;
            s2.Number = s1.Number;
        }
        public static void CourseParser( IExternalDataTypes.ICourse c1,  Course c2)
        {
            c2.Name = c1.Name;
            c2.Acronym = c1.Acronym;
            
            //faltam cunits no external 
        }
        public static void CUnitParser( IExternalDataTypes.ICUnit c1,  CUnit c2)
        {
            c2.Name = c1.Name;
            c2.Acronym = c1.Acronym;
        }
        public static void RegimeParser( IExternalDataTypes.IRegime r1,  Regime r2)
        {
            r2.Description = r1.Description;
        }
        public static void RoomParser( IExternalDataTypes.IRoom r1,  Room r2)
        {
            r2.Description = r1.Description;
            r2.Seats = r1.Seats;
        }

        public static void ClassParser( IClass c1, SClass c2)
        {
            c2.Name = c1.Name;
            
        }

        /*public static void ExamParser(IExternalDataTypes.IExam ex1 , ExaminationTT.Model.IExam ex2)
        {
            ex1.CUnit = ex2.CUnit;
            ex1.ExamDate = ex2.ExamEndDate;
            ex1.Rooms = ex2.Rooms;
            ex1.Season = ex2.Season;
        }

        public static void SeasonParser(IExternalDataTypes.IExamSeason ex1, IExamSeason ex2)
        {
            ex1.StartSeason = ex2.StartSeason;
            ex1.EndSeason = ex2.EndSeason;
            ex1.Description = ex2.Description;
        }*/

        #endregion
    }
}
